package net.huashitong.ssydt.provider.article.service;


import net.huashitong.ssydt.PaginationResult;
import net.huashitong.ssydt.article.constant.ArticleConsts;
import net.huashitong.ssydt.article.entity.SsArticleArticle;
import net.huashitong.ssydt.article.entity.SsArticleArticleText;
import net.huashitong.ssydt.article.entity.vo.ArticleArticleInVO;
import net.huashitong.ssydt.article.entity.vo.ArticleArticleOutVO;
import net.huashitong.ssydt.article.entity.vo.ArticleArticleQueryInVO;
import net.huashitong.ssydt.article.entity.vo.ArticleListInVO;
import net.huashitong.ssydt.exceptions.ParamErrorServiceException;
import net.huashitong.ssydt.provider.article.mapper.SsArticleArticleMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author <a href="mailto:yhy23456@163.com">huiyang.yu</a>
 * @since 2017.09.14
 */
@Service
@Transactional
public class SsArticleArticleServiceImpl implements SsArticleArticleService {

    private SsArticleArticleMapper mapper;

    private SsArticleTypeService ssArticleTypeService;

    private SsArticleArticleTextService ssArticleArticleTextService;

    @Autowired
    public SsArticleArticleServiceImpl(SsArticleArticleMapper mapper,
                                       SsArticleTypeService ssArticleTypeService,
                                       SsArticleArticleTextService ssArticleArticleTextService) {
        this.mapper = mapper;
        this.ssArticleTypeService = ssArticleTypeService;
        this.ssArticleArticleTextService = ssArticleArticleTextService;
    }

    @Override
    public int deleteByPrimaryKey(Long id) {
        return mapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(SsArticleArticle record) {
        return mapper.insert(record);
    }

    @Override
    public int insertSelective(SsArticleArticle record) {
        return mapper.insertSelective(record);
    }

    @Override
    public SsArticleArticle selectByPrimaryKey(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(SsArticleArticle record) {
        return mapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(SsArticleArticle record) {
        return mapper.updateByPrimaryKey(record);
    }

    @Override
    public SsArticleArticle getById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public ArticleArticleOutVO getArticle(Long articleId) {
        SsArticleArticle ssArticleArticle = getById(articleId);
        SsArticleArticleText ssArticleArticleText = ssArticleArticleTextService.selectByArticleId(articleId);

        ArticleArticleOutVO articleArticleOutVO = new ArticleArticleOutVO();
        BeanUtils.copyProperties(ssArticleArticle, articleArticleOutVO);
        articleArticleOutVO.setContent(ssArticleArticleText.getContent());
        return articleArticleOutVO;
    }

    @Override
    public PaginationResult<SsArticleArticle> paginationQuery(ArticleArticleQueryInVO articleQueryInVO) {
        PaginationResult result = new PaginationResult();
        result.setPageNumber(articleQueryInVO.getPageNumber());
        result.setPageSize(articleQueryInVO.getPageSize());

        // 查询记录总数
        int total = mapper.paginationCount(articleQueryInVO);
        result.setPageRecords(total);

        if (total == 0) {
            result.setList(new ArrayList());
        } else {
            // 查询当前页记录
            List<SsArticleArticle> list = mapper.paginationQuery(articleQueryInVO);
            result.setList(list);
        }
        return result;
    }

    @Override
    public Long addArticle(ArticleArticleInVO articleArticleInVO) {
        validateArticle(articleArticleInVO);

        // 置顶检查
        int topArticle = articleArticleInVO.getTopArticle();
        if (topArticle >= 1 && topArticle <= 3) {
            ArticleArticleQueryInVO articleArticleQueryInVO = new ArticleArticleQueryInVO();
            articleArticleQueryInVO.setTopArticle(topArticle);
            if (0 != mapper.paginationCount(articleArticleQueryInVO)) {
                throw new ParamErrorServiceException(String.format("置项%d 已被使用", 4 - topArticle));
            }
        }

        // 文章基本信息
        SsArticleArticle ssArticleArticle = new SsArticleArticle();
        BeanUtils.copyProperties(articleArticleInVO, ssArticleArticle);
        ssArticleArticle.setClickNumber(0);
        ssArticleArticle.setCommentNumber(0);
        ssArticleArticle.setHotNumber(System.currentTimeMillis());
        ssArticleArticle.setPraiseNumber(0);
        mapper.insert(ssArticleArticle);

        // 文章内容
        SsArticleArticleText ssArticleArticleText = new SsArticleArticleText();
        ssArticleArticleText.setArticleId(ssArticleArticle.getId());
        ssArticleArticleText.setContent(articleArticleInVO.getContent());
        ssArticleArticleTextService.insert(ssArticleArticleText);

        return ssArticleArticle.getId();
    }

    @Override
    public void updateArticle(ArticleArticleInVO articleArticleInVO) {
        validateArticle(articleArticleInVO);

        // 文章基本信息
        SsArticleArticle ssArticleArticle = getById(articleArticleInVO.getId());

        // 置顶检查
        int topArticle = articleArticleInVO.getTopArticle();
        if (topArticle >= 1 && topArticle <= 3 && topArticle != ssArticleArticle.getTopArticle()) {
            ArticleArticleQueryInVO articleArticleQueryInVO = new ArticleArticleQueryInVO();
            articleArticleQueryInVO.setTopArticle(topArticle);
            if (0 != mapper.paginationCount(articleArticleQueryInVO)) {
                throw new ParamErrorServiceException(String.format("置项%d 已被其他文章占用, 请先取消占用", 4 - topArticle));
            }
        }

        BeanUtils.copyProperties(articleArticleInVO, ssArticleArticle);
        ssArticleArticle.setClickNumber(null);
        ssArticleArticle.setCommentNumber(null);
        ssArticleArticle.setHotNumber(System.currentTimeMillis());
        ssArticleArticle.setPraiseNumber(null);
        mapper.updateByPrimaryKeySelective(ssArticleArticle);

        // 文章内容
        SsArticleArticleText ssArticleArticleText = new SsArticleArticleText();
        ssArticleArticleText.setArticleId(ssArticleArticle.getId());
        ssArticleArticleText.setContent(articleArticleInVO.getContent());
        ssArticleArticleTextService.updateContentByArticleId(ssArticleArticleText);
    }

    @Override
    public void deleteArticle(Long articleId) {
        getById(articleId);
        mapper.deleteByPrimaryKey(articleId);
        ssArticleArticleTextService.deleteByArticleId(articleId);
    }

    @Override
    public List<SsArticleArticle> queryLatelyThreeByIdWithArticleTypeId(SsArticleArticle ssArticleArticle) {
        List<SsArticleArticle> beforeArticles = mapper.findThreeForBeforeByIdWithArticleTypeId(ssArticleArticle);//旧
        List<SsArticleArticle> afterArticles = mapper.findThreeForAfterByIdWithArticleTypeId(ssArticleArticle);//新
        List<SsArticleArticle> list = new ArrayList<>();
        int beforeSize = beforeArticles.size();
        int afterSize = afterArticles.size();
        int size = beforeSize + afterSize;
        if (size > 3) {
            if (beforeSize >= 2) {
                list.add(afterArticles.get(0));
                list.add(beforeArticles.get(0));
                list.add(beforeArticles.get(1));
            } else {
                list.add(afterArticles.get(1));
                list.add(afterArticles.get(0));
                list.add(beforeArticles.get(0));
            }
        } else {
            list.addAll(afterArticles);
            for (SsArticleArticle beforeArticle : beforeArticles) {
                list.add(beforeArticle);
                if (list.size() >= 3) {
                    break;
                }
            }
        }
        return list;
    }

    @Override
    public int incrementClickNumber(Long id) {
        return mapper.incrementClickNumber(id);
    }

    @Override
    public int incrementCommentNumber(Long id, int number) {
        SsArticleArticle ssArticleArticle = new SsArticleArticle();
        ssArticleArticle.setCommentNumber(number);
        ssArticleArticle.setId(id);
        return mapper.incrementCommentNumber(ssArticleArticle);
    }

    @Override
    public int incrementHotNumber(Long id, long number) {
        SsArticleArticle ssArticleArticle = new SsArticleArticle();
        ssArticleArticle.setHotNumber(number);
        ssArticleArticle.setId(id);
        return mapper.incrementHotNumber(ssArticleArticle);
    }

    @Override
    public void updateClickNumberAndHotNumber(Long id) {
        this.incrementClickNumber(id);//点击数自增
        this.incrementHotNumber(id, ArticleConsts.HOT_NUMBER_CLICK);
    }

    @Override
    public int incrementPraiseNumber(Long articleId) {
        return this.incrementPraiseNumber(articleId, 1);
    }

    @Override
    public int incrementPraiseNumber(Long articleId, int number) {
        SsArticleArticle ssArticleArticle = new SsArticleArticle();
        ssArticleArticle.setId(articleId);
        ssArticleArticle.setPraiseNumber(number);
        return this.mapper.incrementPraiseNumber(ssArticleArticle);
    }


    @Override
    public List<SsArticleArticle> query(ArticleListInVO articleListInVO) {
        return mapper.query(articleListInVO);
    }

    private void validateArticle(ArticleArticleInVO articleArticleInVO) {
        ssArticleTypeService.getById(articleArticleInVO.getArticleTypeId());

        // 置顶检查
        int topArticle = articleArticleInVO.getTopArticle();
        if (topArticle > 3) {
            throw new ParamErrorServiceException("置顶输入错误");
        }
    }

}
